package com.hrm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrm.mapper.DeptMapper;
import com.hrm.pojo.DTO.DeptPageDTO;
import com.hrm.pojo.Dept;
import com.hrm.service.DeptService;
import com.hrm.utils.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    /**
     * 部门列表
     * @param deptPageDTO
     * @return
     */
    public PageResult<Dept> page(DeptPageDTO deptPageDTO) {
        //开启分页
        Page<Dept> deptPage = PageHelper.startPage(deptPageDTO.getPage(), deptPageDTO.getPageSize());
        //根据部门名称动态查询数据
        List<Dept> list = deptMapper.selectLikeName(deptPageDTO.getName());
        return new PageResult<Dept>(deptPage.getTotal(),list);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    public Boolean deleteById(Integer id) {
        return deptMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    public Boolean insertDept(Dept dept) {
        return deptMapper.insertSelective(dept)>0;
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    public Boolean updateById(Dept dept) {
        return deptMapper.updateByPrimaryKeySelective(dept)>0;
    }

    public List<Dept> getAllDept() {
        return deptMapper.getAllDept();
    }
}
